﻿//----------------------------------------------------

#region 命名空间

//    版权：
//    作者：
//建立时间：2012-09-11 00:00:33
//    描述：
//----------------------------------------------------
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using WinnerCMS.Common;
using WinnerCMS.Data;
using WinnerCMS.Model;
using WinnerCMS.Model.Enum;

#endregion

namespace WinnerCMS.BLL
{
    /// <summary>
    /// 业务逻辑类 WinnerCMS_Field 的摘要说明。
    /// </summary>
    public class FieldManage
    {
        private static readonly FieldDal MyDAL = new FieldDal();

        #region 添加

        /// <summary>
        /// 添加
        /// </summary>
        public void Add(Field model, ref string strErr)
        {
            if (model.FieldName.Length == 0)
            {
                strErr = "“字段名称”不能为空！";
                return;
            }
            if (model.Title.Length == 0)
            {
                strErr = "“字段标题”不能为空！";
                return;
            }

            if (model.FieldType.Length == 0)
            {
                strErr = "“字段类型”不能为空！";
                return;
            }

            if (model.Id == 0)
            {
                MyDAL.Add(model);

                ModelType BaseModel = ModelManage.GetBaseModel(model.ModelId);

                if (BaseModel != ModelType.Shop && BaseModel != ModelType.Ask)
                {
                    new FieldObject(model.ModelId).Add(model);
                }
            }
            else
            {
                MyDAL.Edit(model);
            }
        }

        #endregion

        #region 删除

        /// <summary>
        /// 删除
        /// </summary>
        public void Delete(byte ModelId, int ID, UpdatePanel UpdatePanel1)
        {
            Field model = MyDAL.GetInfo(ID);
            if (model != null)
            {
                ModelType BaseModel = ModelManage.GetBaseModel(ModelId);

                if (BaseModel != ModelType.Shop && BaseModel != ModelType.Ask)
                {
                    try
                    {
                        MyDAL.DeleteField(ModelManage.GetTableName(ModelId), model.FieldName);
                    }
                    catch
                    {
                    }
                }
                MyDAL.Delete(ID);

                MsgBox.Message("删除成功!", UpdatePanel1);
            }
            else
            {
                MsgBox.Message("字段已经不存在。", UpdatePanel1);
            }
        }

        #endregion

        #region 加载数据

        /// <summary>
        /// 加载数据
        /// </summary>
        public Field GetInfo(int ID)
        {
            return MyDAL.GetInfo(ID);
        }

        #endregion

        #region 绑定数据

        /// <summary>
        /// 绑定分页数据
        /// </summary>
        /// <param name="pp"></param>
        /// <returns></returns>
        public IList<Field> BindList(byte ModelId, PagingParam pp)
        {
            return MyDAL.BindList(ModelId, pp);
        }

        #endregion

        #region 替换

        /// <summary>
        /// 替换
        /// </summary>
        /// <param name="ModelId"></param>
        /// <param name="CategoryId"></param>
        /// <param name="Fields"></param>
        /// <param name="OldString"></param>
        /// <param name="NewString"></param>
        /// <param name="ReplaceType"></param>
        public void RepalceString(byte ModelId, string CategoryId, string Fields, string OldString, string NewString,
                                  int ReplaceType)
        {
            OldString = MyString.SQL_ESC(OldString);
            NewString = MyString.SQL_ESC(NewString);

            MyDAL.RepalceString(ModelId, CategoryId, Fields, OldString, NewString, ReplaceType);
        }

        #endregion

        #region  加载扩展字段的数据

        /// <summary>
        /// 加载扩展字段的数据
        /// </summary>
        /// <param name="ModelId"></param>
        /// <param name="IsShowInMember"></param>
        /// <param name="ContentDataKey"></param>
        /// <param name="T_DefinedFile"></param>
        public void ExtendFieldData(byte ModelId, bool IsShowInMember, string ContentDataKey, Table T_DefinedFile,
                                    ref string strErr)
        {
            string TableName = ModelManage.GetTableName(ModelId);
            DataRow dr = null;
            try
            {
                dr = MyDAL.ExtendFieldData(ModelId, IsShowInMember, TableName, int.Parse(ContentDataKey));
            }
            catch (Exception ex)
            {
                strErr = ex.Message;
                return;
            }

            if (dr == null) return;

            for (int i = 0; i < T_DefinedFile.Rows.Count; i++)
            {
                TableRow tr = T_DefinedFile.Rows[i];
                Control c = tr.Cells[1].Controls[0];
                if (c is TextBox)
                {
                    TextBox tb = c as TextBox;
                    tb.Text = dr[tb.ID].ToString();
                }
                if (c is DropDownList)
                {
                    DropDownList ddl = c as DropDownList;
                    ddl.SelectedValue = dr[ddl.ID].ToString();
                }
                if (c is RadioButtonList)
                {
                    RadioButtonList rbl = c as RadioButtonList;
                    rbl.SelectedValue = dr[rbl.ID].ToString();
                }
                if (c is CheckBox)
                {
                    CheckBox cb = c as CheckBox;
                    try
                    {
                        cb.Checked = Convert.ToBoolean(dr[cb.ID]);
                    }
                    catch
                    {
                    }
                }
            }
        }

        /// <summary>
        /// 加载扩展字段的数据
        /// </summary>
        /// <param name="CategoryId"></param>
        /// <param name="T_DefinedFile"></param>
        public void ExtendFieldData(int GoodsID, Table T_DefinedFile)
        {
            var dt = new GoodsCustomFieldValuesDal().ExtendFieldData(GoodsID);

            if (dt.Count == 0) return;

            for (int i = 0; i < T_DefinedFile.Rows.Count; i++)
            {
                TableRow tr = T_DefinedFile.Rows[i];
                Control c = tr.Cells[1].Controls[0];

                var drs = dt.FirstOrDefault(x => x.FieldName == c.ID);
                if (drs == null)
                {
                    continue;
                }


                if (c is TextBox)
                {
                    TextBox tb = c as TextBox;
                    tb.Text = drs.FieldValue;
                }
                if (c is DropDownList)
                {
                    DropDownList ddl = c as DropDownList;
                    ddl.SelectedValue = drs.FieldValue;
                }
                if (c is RadioButtonList)
                {
                    RadioButtonList rbl = c as RadioButtonList;
                    rbl.SelectedValue = drs.FieldValue;
                }
                if (c is CheckBox)
                {
                    CheckBox cb = c as CheckBox;
                    try
                    {
                        cb.Checked = Convert.ToBoolean(drs.FieldValue);
                    }
                    catch
                    {
                    }
                }
            }
        }

        #endregion

        #region 取得商城模型的栏目的扩展字段

        /// <summary>
        /// 取得商城模型的栏目的扩展字段
        /// </summary>
        /// <param name="ModelId"></param>
        /// <returns></returns>
        public IList<Field> GetShopModelField(byte ModelId)
        {
            return MyDAL.GetShopModelField(ModelId);
        }

        /// <summary>
        /// 取得商城模型的栏目的扩展字段
        /// </summary>
        /// <param name="ModelId"></param>
        /// <param name="CategoryId"></param>
        /// <returns></returns>
        public IList<Field> GetShopModelField(byte ModelId, int CategoryId)
        {
            return MyDAL.GetShopModelField(ModelId, CategoryId);
        }

        #endregion
    }
}